package com.homegoods.resource;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kedll.library.util.MyUtils;


/**
 * 设置RecycleView间的间距
 *
 * @author 11470
 */
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;
    private int itemCount;

    public SpacesItemDecoration(int itemCount, int space) {
        this.itemCount = itemCount;
        //屏幕宽度
        this.space = MyUtils.dp2px(space);

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.bottom = space;
        if (parent.getChildLayoutPosition(view) % itemCount == 0) {
            outRect.left = 0;
            outRect.right = space / 2;
        } else if (parent.getChildLayoutPosition(view) % itemCount == itemCount - 1) {
            outRect.right = 0;
            outRect.left = space / 2;
        } else {
            outRect.right = space / 2;
            outRect.left = space / 2;
        }
    }
}